package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.Archives;
import cn.stylefeng.guns.sys.modular.system.entity.BtyArchives;
import cn.stylefeng.guns.sys.modular.system.entity.BtyReemploy;
import cn.stylefeng.guns.sys.modular.system.mapper.ArchivesMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyArchivesMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.ArchivesParam;
import cn.stylefeng.guns.sys.modular.system.model.result.ArchivesResult;
import  cn.stylefeng.guns.sys.modular.system.service.ArchivesService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxc
 * @since 2021-05-13
 */
@Service
public class ArchivesServiceImpl extends ServiceImpl<ArchivesMapper, Archives> implements ArchivesService {

    @Resource
    private BtyArchivesMapper btyArchivesMapper;

    @Override
    public void add(ArchivesParam param){
        Archives entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ArchivesParam param){
//        this.removeById(getKey(param));
        this.baseMapper.update(param);
    }

    @Override
    public void update(ArchivesParam param){
        Archives oldEntity = getOldEntity(param);
        Archives newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ArchivesResult findBySpec(ArchivesParam param){
        return null;
    }

    @Override
    public List<ArchivesResult> findListBySpec(ArchivesParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ArchivesParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ArchivesParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Archives getOldEntity(ArchivesParam param) {
        return this.getById(getKey(param));
    }

    private Archives getEntity(ArchivesParam param) {
        Archives entity = new Archives();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 恢复档案
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void recovery(ArchivesParam param) {
        int i = this.baseMapper.update(param);//离职表中删除
        if (i > 0){
            Archives archives = this.baseMapper.selectById(param.getId());
            //查询档案表中是否存在该数据
            int con = btyArchivesMapper.getCon(archives.getBh());
            if (con <= 0){
                BtyArchives btyArchives = new BtyArchives();
                BeanUtil.copyProperties(archives, btyArchives, new String[]{"id"});
                btyArchivesMapper.insert(btyArchives);
            }

        }
    }

    @Override
    public List<ArchivesResult> getLeaveEmpList(List<BtyReemploy> reemploys) {
        return this.baseMapper.getLeaveEmpList(reemploys);
    }

    @Override
    public Object listExport() {
        List<ArchivesResult> list = this.baseMapper.listExport();
        return list;
    }

}
